package com.balance.service.sys.impl;

import com.balance.mapper.sys.SysUserMapper;
import com.balance.pojo.sys.SysUser;
import com.balance.service.sys.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Title: SysUserServiceImpl
 * @Author 小东
 * @Package com.balance.service.impl
 * @Date 2025/3/27 18:01
 * @description: 系统用户业务层
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> getAll(SysUser sysUser) {
        List<SysUser> all = sysUserMapper.getAll(sysUser);
        if(all.size() <= 0) throw new IllegalArgumentException("未查询到此人");
        return all;
    }

    @Override
    public SysUser getUserById(Integer id) {
        SysUser userById = sysUserMapper.getUserById(id);
        if(userById == null ) throw new IllegalArgumentException("未查询到此人");
        return userById;
    }

    @Override
    public SysUser selectUserAndRoleList(Map<String, Object> params) {
        return sysUserMapper.selectUserAndRoleList(params);
    }

    @Transactional
    @Override
    public int insertUser(SysUser user) {
        return sysUserMapper.insertUser(user);
    }

    @Override
    public int updateUser(SysUser user) {
        return sysUserMapper.updateUser(user);
    }

    @Override
    public int deleteUserById(Long id) {
        return sysUserMapper.deleteUserById(id);
    }

    @Override
    public SysUser getSysUserByUsername(SysUser sysUser){
        if(sysUser.getUsername() == null || sysUser.getPassword() == null){
            return null;
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,sysUser.getUsername() ).eq(SysUser::getPassword,sysUser.getPassword());
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SysUser> getUserByMobile(String mobile) {
        SysUser sysUser = new SysUser();
        sysUser.setMobile(mobile);
        return sysUserMapper.getAll(sysUser);
    }
}